package com.mxpio.erp.equipment.job;

import com.mxpio.erp.common.equipment.entity.EquipmentCheckTask;
import com.mxpio.erp.common.equipment.service.EquipmentCheckTaskService;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Classname TaskReleaseJob
 * @Description 任务自动下达
 */
@Slf4j
@Configurable
@EnableScheduling
@Component
public class CheckTaskReleaseJob {

    public static final Integer STATUS_START = 10;


    @Autowired
    private EquipmentCheckTaskService tpmCheckTaskService;


    /**
     * 自动下达点检任务（每一分钟）
     */
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void executeCheckTaskReleaseJob(){
        /**
         * 查询任务中需要下达的工单
         * 1.待下达的
         * 2.需要自动下达的
         * 3.下达时间在当前时间之后的
         */
        Criteria c = Criteria.create().addCriterion("orderStatus", Operator.EQ, STATUS_START);
        c.addCriterion("isAutoRelease", Operator.EQ, 1);
        c.addCriterion("planReleaseTime", Operator.LE, new Date());
        List<EquipmentCheckTask> list = tpmCheckTaskService.list(EquipmentCheckTask.class,c);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        for (EquipmentCheckTask tpmCheckTask : list) {

            try {
                tpmCheckTaskService.release(tpmCheckTask.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
